<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>vite</title>
</head>
<body>
    <script src="./index.js"></script>
    <script>
        function myPromiseAll(iterable) {
  return new Promise((resolve, reject) => {
    // 1. 参数校验：非可迭代对象抛出错误
    if (!Symbol.iterator in iterable) {
      throw new TypeError('参数必须是可迭代对象');
    }

    // 2. 转换为数组，处理空数组
    const promises = Array.from(iterable);
    if (promises.length === 0) {
      resolve([]); // 空数组直接resolve
      return;
    }

    // 3. 初始化状态
    const results = new Array(promises.length); // 保存结果，顺序与输入一致
    let completedCount = 0; // 已完成的任务数
    let isRejected = false; // 防止多次reject

    // 4. 遍历处理每个任务
    for (let i = 0; i < promises.length; i++) {
      const currentPromise = Promise.resolve(promises[i]); // 包装为Promise

      currentPromise
        .then((value) => {
          results[i] = value; // 按索引保存结果
          completedCount++; // 计数器+1

          // 所有任务完成时，resolve结果数组
          if (completedCount === promises.length) {
            resolve(results);
          }
        })
        .catch((error) => {
          if (!isRejected) { // 确保只处理第一个错误
            isRejected = true;
            reject(error); // 立即reject
          }
        });
    }
  });
}
    </script>
</body>
</html>